package com.zsck.pos.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.AttrRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.Toolbar;


import com.chinaums.commoncheck.R;
import com.zsck.pos.utils.ResourceUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TitleBar extends FrameLayout {


    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rl_right)
    RelativeLayout rlRight;
    @BindView(R.id.ll_top)
    RelativeLayout llTop;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.ll_base)
    LinearLayout llBase;


    public TitleBar(@NonNull Context context) {
        this(context, null);
    }

    public TitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBar(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initViews(attrs);
    }

    private Context context;
    private OnClickListener listener;

    private void initViews(AttributeSet attrs) {
        View.inflate(context, R.layout.widge_title_bar_layout, this);
        ButterKnife.bind(this);
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TitleBar, 0, 0);
            try {
                String t = ta.getString(R.styleable.TitleBar_tb_title);
                tvTitle.setText(t);
                boolean rightIconVisible = ta.getBoolean(R.styleable.TitleBar_tb_right_icon_visible, false);
                if (rightIconVisible) {
                    int rightIconResId = ta.getResourceId(R.styleable.TitleBar_tb_right_icon, 0);
                    setRightIcon(rightIconResId);
                }


            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                ta.recycle();
            }
        }
    }


    public void setRightIcon(@DrawableRes int drawableResId) {
        ivRight.setImageResource(drawableResId);
    }


    @OnClick({R.id.rl_back, R.id.rl_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                if (listener != null) {
                    listener.onLeftClick();
                }
                break;
            case R.id.rl_right:
                if (listener != null) {
                    listener.onRightClick();
                }
                break;
        }
    }

    public interface OnClickListener {

        void onLeftClick();

        void onRightClick();

    }

    public void setOnClickListener(OnClickListener listener) {
        this.listener = listener;
    }

    public void setTitle(CharSequence t) {
        if (TextUtils.isEmpty(t)) {
            tvTitle.setVisibility(GONE);
        } else {
            if (tvTitle.getVisibility() != VISIBLE) {
                tvTitle.setVisibility(VISIBLE);
            }
            tvTitle.setText(t);
        }
    }

    public void setTitle(@StringRes int strResId) {
        setTitle(ResourceUtils.getString(getContext(), strResId));
    }


}
